<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>lodash常用API | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,lodash,">
  

  <meta name="description" content="中文文档 https://www.lodashjs.com/docs/4.17.5.html  _.chunk 将数组进行切分   这个函数把数组按照一定的长度分开，返回新的数组。（片段化数组）  const arr = [1,2,3,4,5,6,7,8,9];_.chunk(arr,2);// =&amp;gt;[[1,2],[3,4],[5,6],[7,8],[9]] compact 去除假值。（将">
<meta name="keywords" content="JavaScript,lodash">
<meta property="og:type" content="article">
<meta property="og:title" content="lodash常用API">
<meta property="og:url" content="http://blog.poetries.top/2018/12/06/lodash-api/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="中文文档 https://www.lodashjs.com/docs/4.17.5.html  _.chunk 将数组进行切分   这个函数把数组按照一定的长度分开，返回新的数组。（片段化数组）  const arr = [1,2,3,4,5,6,7,8,9];_.chunk(arr,2);// =&amp;gt;[[1,2],[3,4],[5,6],[7,8],[9]] compact 去除假值。（将">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="lodash常用API">
<meta name="twitter:description" content="中文文档 https://www.lodashjs.com/docs/4.17.5.html  _.chunk 将数组进行切分   这个函数把数组按照一定的长度分开，返回新的数组。（片段化数组）  const arr = [1,2,3,4,5,6,7,8,9];_.chunk(arr,2);// =&amp;gt;[[1,2],[3,4],[5,6],[7,8],[9]] compact 去除假值。（将">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#chunk"><span class="toc-text">_.chunk</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#compact"><span class="toc-text">compact</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#uniq"><span class="toc-text">uniq</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#reject"><span class="toc-text">_.reject</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#shuffle"><span class="toc-text">shuffle</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#pick"><span class="toc-text">_.pick</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#keys"><span class="toc-text">_.keys</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#cloneDeep"><span class="toc-text">_.cloneDeep</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#forEach"><span class="toc-text">_.forEach</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#merge-参数合并"><span class="toc-text">_.merge 参数合并</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#random"><span class="toc-text">_.random</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#find查找数组"><span class="toc-text">_.find查找数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#keyBy"><span class="toc-text">_.keyBy</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#filter"><span class="toc-text">_.filter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-text">_.map</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#max-min-sum"><span class="toc-text">.max/.min/_.sum</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#pad-padStart-padEnd"><span class="toc-text">.pad/.padStart/_.padEnd</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#选出json数组中id最大的一项"><span class="toc-text">选出json数组中id最大的一项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#更新json数组中某一项的值"><span class="toc-text">更新json数组中某一项的值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#扁平化数组-flatten"><span class="toc-text">扁平化数组 _.flatten</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#数组去重"><span class="toc-text">数组去重</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-lodash-api" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">lodash常用API</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.12.06</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 5分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <blockquote>
<p>中文文档 <a href="https://www.lodashjs.com/docs/4.17.5.html" target="_blank" rel="noopener">https://www.lodashjs.com/docs/4.17.5.html</a></p>
</blockquote>
<h3 id="chunk"><a href="#chunk" class="headerlink" title="_.chunk"></a>_.chunk</h3><blockquote>
<p>将数组进行切分</p>
</blockquote>
<ul>
<li>这个函数把数组按照一定的长度分开，返回新的数组。（片段化数组）</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>];</span><br><span class="line">_.chunk(arr,<span class="number">2</span>);</span><br><span class="line"><span class="comment">// =&gt;[[1,2],[3,4],[5,6],[7,8],[9]]</span></span><br></pre></td></tr></table></figure>
<h3 id="compact"><a href="#compact" class="headerlink" title="compact"></a>compact</h3><blockquote>
<p>去除假值。（将所有的空值，0，NaN过滤掉）</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">_.compact([<span class="string">'1'</span>,<span class="string">'2'</span>,<span class="string">' '</span>,<span class="number">0</span>])</span><br><span class="line"><span class="comment">// =&gt; ['1','2']</span></span><br></pre></td></tr></table></figure>
<h3 id="uniq"><a href="#uniq" class="headerlink" title="uniq"></a>uniq</h3><blockquote>
<p>数组去重。（将数组中的对象去重，只能是数组去重，不能是对象去重。）</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">_.uniq([<span class="number">1</span>,<span class="number">1</span>,<span class="number">3</span>])</span><br><span class="line"><span class="comment">// =&gt; [1,3]</span></span><br></pre></td></tr></table></figure>
<h3 id="reject"><a href="#reject" class="headerlink" title="_.reject"></a>_.reject</h3><blockquote>
<p>根据条件去除某个元素</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.reject(foo, [<span class="string">'id'</span>, <span class="number">0</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment">//bar = [&#123;id: 1, name: "bbb", age: 25&#125;]</span></span><br></pre></td></tr></table></figure>
<h3 id="shuffle"><a href="#shuffle" class="headerlink" title="shuffle"></a>shuffle</h3><blockquote>
<p>无序化</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">_.shuffle([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]);<span class="comment">// 无序化</span></span><br></pre></td></tr></table></figure>
<h3 id="pick"><a href="#pick" class="headerlink" title="_.pick"></a>_.pick</h3><blockquote>
<p>根据key来筛选数组</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;</span><br><span class="line"><span class="keyword">var</span> bar = _.pick(foo, [<span class="string">'name'</span>, <span class="string">'age'</span>])</span><br><span class="line"><span class="comment">//bar = &#123;name: "aaa", age: 33&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="keys"><a href="#keys" class="headerlink" title="_.keys"></a>_.keys</h3><blockquote>
<p>返回object中的所有key</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;</span><br><span class="line"><span class="keyword">var</span> bar = _.keys(foo)</span><br><span class="line"><span class="comment">//bar = ['id', 'name', 'age']</span></span><br></pre></td></tr></table></figure>
<h3 id="cloneDeep"><a href="#cloneDeep" class="headerlink" title="_.cloneDeep"></a>_.cloneDeep</h3><blockquote>
<p>深度拷贝</p>
</blockquote>
<h3 id="forEach"><a href="#forEach" class="headerlink" title="_.forEach"></a>_.forEach</h3><p><strong>常规数组遍历</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">agent.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">n,key</span>) </span>&#123;</span><br><span class="line">    agent[key].agent_id = agent[key].agent_name;</span><br><span class="line">    <span class="keyword">return</span> agent;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p><strong>使用lodash来遍历</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">_.forEach(agent,<span class="function"><span class="keyword">function</span>(<span class="params">n,key</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    agent[key].agent_id= agent[key].agent_name</span><br><span class="line"></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这是一个常见的<code>forEach</code>的数组遍历，使用了<code>lodash</code>过后，<code>_.forEach()</code>这是一个值，而不是一个函数。就可以直接</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = _.forEach();</span><br></pre></td></tr></table></figure>
<p>这时候<code>arr</code>就是新的数组<code>agent</code></p>
<h3 id="merge-参数合并"><a href="#merge-参数合并" class="headerlink" title="_.merge 参数合并"></a>_.merge 参数合并</h3><blockquote>
<p>递归的将源对象和继承的可枚举字符串监控属性合并到目标对象中。源对象从左到右引用，后续来源将覆盖以前来源的属性分配。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> object = &#123;</span><br><span class="line">  <span class="string">'a'</span>: [&#123; <span class="string">'b'</span>: <span class="number">2</span> &#125;, &#123; <span class="string">'d'</span>: <span class="number">4</span> &#125;]</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">var</span> other = &#123;</span><br><span class="line">  <span class="string">'a'</span>: [&#123; <span class="string">'c'</span>: <span class="number">3</span> &#125;, &#123; <span class="string">'e'</span>: <span class="number">5</span> &#125;]</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line">_.merge(object, other);</span><br><span class="line"><span class="comment">// =&gt; &#123; 'a': [&#123; 'b': 2, 'c': 3 &#125;, &#123; 'd': 4, 'e': 5 &#125;] &#125;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>在实际开发中，前端在接口的请求可以<code>merge</code>一下之前的<code>query</code>和现在改变的查询的值，再去请求后端接口的数据</p>
</blockquote>
<h3 id="random"><a href="#random" class="headerlink" title="_.random"></a>_.random</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(_.random(<span class="number">10</span>, <span class="number">20</span>)); <span class="comment">// 获取随机数</span></span><br><span class="line">_.random(<span class="number">15</span>, <span class="number">20</span>, <span class="literal">true</span>); <span class="comment">// 随机浮点</span></span><br><span class="line">_.sample([<span class="string">"lisong"</span>, <span class="string">"heyan"</span>], <span class="number">1</span>);<span class="comment">// 随机获取数组中的某一项,attr2:随机获取的个数</span></span><br></pre></td></tr></table></figure>
<h3 id="find查找数组"><a href="#find查找数组" class="headerlink" title="_.find查找数组"></a>_.find查找数组</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.find(foo, [<span class="string">'id'</span>, <span class="number">0</span>])</span><br><span class="line"><span class="comment">//bar = &#123;id: 0, name: "aaa", age: 33&#125;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意一下如果没找到的话，会返回<code>undefined</code>，要处理一下</p>
</blockquote>
<h3 id="keyBy"><a href="#keyBy" class="headerlink" title="_.keyBy"></a>_.keyBy</h3><blockquote>
<p>以某个属性为键，将数组转为对象</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = <span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.keyBy(foo, <span class="string">'name'</span>)</span><br><span class="line"><span class="comment">//bar = &#123;</span></span><br><span class="line"><span class="comment">//    aaa: &#123;id: 0, name: "aaa", age: 33&#125;,</span></span><br><span class="line"><span class="comment">//    bbb: &#123;id: 1, name: "bbb", age: 25&#125;</span></span><br><span class="line"><span class="comment">//&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="filter"><a href="#filter" class="headerlink" title="_.filter"></a>_.filter</h3><blockquote>
<p>根据条件过滤出符合条件的元素，返回新数组</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = <span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.filter(foo, [<span class="string">'name'</span>, <span class="string">"aaa"</span>])</span><br><span class="line"><span class="comment">//bar = &#123;</span></span><br><span class="line"><span class="comment">//    aaa: &#123;id: 0, name: "aaa", age: 33&#125;</span></span><br><span class="line"><span class="comment">//&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="map"><a href="#map" class="headerlink" title="_.map"></a>_.map</h3><blockquote>
<p>从集合中挑出一个<code>key</code>，将其值作为数组返回</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = <span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.map(foo, <span class="string">'name'</span>)</span><br><span class="line"><span class="comment">//bar = ["aaa", "bbb"]</span></span><br></pre></td></tr></table></figure>
<h3 id="max-min-sum"><a href="#max-min-sum" class="headerlink" title=".max/.min/_.sum"></a><em>.max/</em>.min/_.sum</h3><blockquote>
<p>数组中最大值、最小值、数组求和</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="keyword">var</span> bar = _.max(foo)</span><br><span class="line"><span class="comment">//bar = 4</span></span><br><span class="line">bar = _.min(foo)</span><br><span class="line"><span class="comment">//bar = 1</span></span><br><span class="line">bar = _.sum(foo)</span><br><span class="line"><span class="comment">//bar = 10</span></span><br></pre></td></tr></table></figure>
<h3 id="pad-padStart-padEnd"><a href="#pad-padStart-padEnd" class="headerlink" title=".pad/.padStart/_.padEnd"></a><em>.pad/</em>.padStart/_.padEnd</h3><blockquote>
<p>在两端、开头、末尾补齐字符</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = <span class="string">"helloworld"</span></span><br><span class="line"><span class="keyword">var</span> bar = _.pad(foo, <span class="number">14</span>, <span class="string">'-'</span>)</span><br><span class="line"><span class="comment">//bar = --helloworld--</span></span><br><span class="line">bar = _.padStart(foo, <span class="number">14</span>, <span class="string">'-'</span>)</span><br><span class="line"><span class="comment">//bar = ----helloworld</span></span><br><span class="line">bar = _.padEnd(foo, <span class="number">14</span>, <span class="string">'-'</span>)</span><br><span class="line"><span class="comment">//bar = helloworld----</span></span><br></pre></td></tr></table></figure>
<h3 id="选出json数组中id最大的一项"><a href="#选出json数组中id最大的一项" class="headerlink" title="选出json数组中id最大的一项"></a>选出json数组中id最大的一项</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">var</span> bar = _.find(foo, [<span class="string">'id'</span>, _.max(_.map(foo, <span class="string">'id'</span>))])</span><br><span class="line"><span class="comment">// bar = &#123;id: 1, name: "bbb", age: 25&#125;</span></span><br><span class="line"><span class="comment">// ps:也可以用maxBy某个key来代替</span></span><br></pre></td></tr></table></figure>
<h3 id="更新json数组中某一项的值"><a href="#更新json数组中某一项的值" class="headerlink" title="更新json数组中某一项的值"></a>更新json数组中某一项的值</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>, <span class="attr">name</span>: <span class="string">"aaa"</span>, <span class="attr">age</span>: <span class="number">33</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">"bbb"</span>, <span class="attr">age</span>: <span class="number">25</span>&#125;</span><br><span class="line">]</span><br><span class="line"><span class="keyword">let</span> list = _.keyBy(foo, <span class="string">'id'</span>)</span><br><span class="line">list[<span class="number">0</span>].name = <span class="string">"ccc"</span></span><br><span class="line"><span class="keyword">var</span> bar = _.map(list)</span><br><span class="line"><span class="comment">// bar = [</span></span><br><span class="line"><span class="comment">//    &#123;id: 0, name: "ccc", age: 33&#125;,</span></span><br><span class="line"><span class="comment">//    &#123;id: 1, name: "bbb", age: 25&#125;</span></span><br><span class="line"><span class="comment">//]</span></span><br></pre></td></tr></table></figure>
<h3 id="扁平化数组-flatten"><a href="#扁平化数组-flatten" class="headerlink" title="扁平化数组 _.flatten"></a>扁平化数组 _.flatten</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,[<span class="number">3</span>,<span class="number">4</span>,[<span class="number">5</span>],<span class="number">6</span>]]</span><br><span class="line"><span class="keyword">var</span> a8 = _.flatten(arr)  <span class="comment">//[ 1, 2, 3, 4, [ 5 ], 6 ]</span></span><br><span class="line"><span class="keyword">var</span> a9 = _.flattenDeep(arr)  <span class="comment">//[ 1, 2, 3, 4, 5, 6 ]</span></span><br><span class="line"><span class="keyword">var</span> a10 = _.flattenDepth(arr,<span class="number">2</span>)  <span class="comment">//[ 1, 2, 3, 4, 5, 6 ]</span></span><br></pre></td></tr></table></figure>
<h3 id="数组去重"><a href="#数组去重" class="headerlink" title="数组去重"></a>数组去重</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr2 = [<span class="number">12</span>,<span class="number">14</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">12</span>,<span class="number">1</span>,<span class="number">14</span>,<span class="number">16</span>,<span class="number">17</span>,<span class="number">22</span>,<span class="number">2</span>,<span class="number">11</span>,<span class="number">12</span>]</span><br><span class="line"><span class="keyword">var</span> a11 = <span class="built_in">Array</span>.from(<span class="keyword">new</span> <span class="built_in">Set</span>(arr2))</span><br><span class="line"><span class="keyword">var</span> a12 = [...new <span class="built_in">Set</span>(arr2)]</span><br><span class="line"><span class="keyword">var</span> a13 = _.uniq(arr2)  <span class="comment">//[ 12, 14, 11, 1, 16, 17, 22, 2 ]</span></span><br><span class="line"><span class="keyword">var</span> arrObj = [&#123; <span class="string">'x'</span>: <span class="number">1</span>, <span class="string">'y'</span>: <span class="number">2</span> &#125;, &#123; <span class="string">'x'</span>: <span class="number">2</span>, <span class="string">'y'</span>: <span class="number">1</span> &#125;,&#123; <span class="string">'x'</span>: <span class="number">2</span>, <span class="string">'y'</span>: <span class="number">1</span> &#125;, &#123; <span class="string">'x'</span>: <span class="number">1</span>, <span class="string">'y'</span>: <span class="number">2</span> &#125;];</span><br><span class="line"><span class="keyword">var</span> a14 = _.uniqWith(arrObj, _.isEqual)  <span class="comment">//[ &#123; x: 1, y: 2 &#125;, &#123; x: 2, y: 1 &#125; ]</span></span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/12/05/es6-promise-promiseAll/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/12/06/moment-api/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
